const {Comment, Op, Bike} = require("../db/seq");

class CommentServe {
    async addCommentServe(params) {
        const res = await Comment.create({
            ...params
        });
        return res.dataValues;
    }

    async getCommentListServe(ctx) {
        const {pageSize = 10, pageNum = 1, bikeId, content, bikeName} = ctx.request.query;
        const url = ctx.request.url;
        const whereOptArr = []
        const whereOpt = {
            source: url.includes('wx') ? 'weixin' : 'zfb'
        }
        content && Object.assign(whereOpt, {
            content: {
                // 模糊查询
                [Op.like]: '%' + content.trim() + '%'
            }
        });
        bikeName && Object.assign(whereOpt, {
            bikeName: {
                // 模糊查询
                [Op.like]: '%' + bikeName.trim() + '%'
            }
        });
        bikeId && Object.assign(whereOpt, {
            bikeId
        });
        whereOptArr.push(whereOpt)
        const offset = (pageNum - 1) * pageSize;
        const {count, rows} = await Comment.findAndCountAll({
            attributes: {exclude: ['updatedTime']},
            where: whereOptArr.length > 1 ? {
                [Op.and]: [
                    ...whereOptArr
                ]
            } : whereOpt,
            order: [
                ['createdTime', 'DESC']
            ],
            //分页
            offset,
            //限制
            limit: Number(pageSize),
            // include: {
            //     model: Bike
            // }
        })
        return {
            total: count,
            data: rows,
            pageNum,
            pageSize
        }
    }

    async getBikeCommentListServe(bikeId, isWx) {
        const whereOptArr = []
        const whereOpt = {
            source: isWx ? 'weixin' :'zfb'
        }
        bikeId && Object.assign(whereOpt, {
            bikeId
        });
        whereOptArr.push(whereOpt)
        const {count, rows} = await Comment.findAndCountAll({
            attributes: {exclude: ['updatedTime']},
            where: whereOptArr.length > 1 ? {
                [Op.and]: [
                    ...whereOptArr
                ]
            } : whereOpt,
            order: [
                ['createdTime', 'DESC']
            ],
            //分页
            offset: 0,
            //限制
            limit: 3,
            include: {
                model: Bike
            }
        })
        return {
            total: count,
            data: rows,
            // pageNum,
            // pageSize
        }
    }

    async getCommentByUserIdServe({id, createdUserId, isAll}) {
        // getCommentByUserIdServe
        let whereOpt = {}
        createdUserId && Object.assign(whereOpt, {
            createdUserId
        })
        id && Object.assign(whereOpt, {
            id
        })
        let res
        if (isAll) {
            res = await Comment.findAll({
                attributes: {exclude: ['createdByUserId']},
                where: whereOpt
            })
        } else {
            res = await Comment.findOne({
                attributes: {exclude: ['createdByUserId']},
                where: whereOpt
            })
        }
        return res
    }

    async deleteCommentServe(id) {
        const res = await Comment.destroy({
            where: {
                id
            }
        });
        return res > 0;
    }
}

module.exports = new CommentServe();
